package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDataChangeListener {
    private RecyclerView rlvStu;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResult = new ArrayList<>();

    private StuAdapter adapter;
    private Button btnAdd;
    private EditText edtKeyWords;
    private Button searchBtn;
    Boolean isResultList =false;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStudentArrayList();
        adapter = new StuAdapter(MainActivity.this,studentArrayList,MainActivity.this);


        rlvStu = findViewById(R.id.rlv_stu);
        rlvStu.setAdapter(adapter);

        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        rlvStu.setLayoutManager(manager);

        btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
        edtKeyWords=findViewById(R.id.edt_keywords);
        searchBtn = findViewById(R.id.btn_search);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keywords = edtKeyWords.getText().toString().trim();
                search(studentArrayList,keywords);
                adapter.changeData(searchResult);
                adapter.notifyDataSetChanged();
                isResultList=true;
            }
        });
    }

    private void search(ArrayList<Student> studentArrayList, String keywords) {
        searchResult.clear();
        for (int i = 0; i < studentArrayList.size(); i++) {
            Student s =  studentArrayList.get(i);
            if (s.getName().contains(keywords)) {
                searchResult.add(s);
            }
        }
    }
    private void initStudentArrayList() {
        Student s1 = new Student(18001,"张一",20,R.drawable.dog1);
        Student s2 = new Student(18002,"张二",23,R.drawable.dog2);
        Student s3 = new Student(18003,"张三",24,R.drawable.dog3);
        Student s4 = new Student(18004,"张四",25,R.drawable.dog4);
        Student s5 = new Student(18005,"张五",30,R.drawable.dog5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);

        studentArrayList.add(s3);

        studentArrayList.add(s4);
        studentArrayList.add(s5);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data==null){
            return;
        }
        if (resultCode==2001){
            int no = data.getIntExtra("NO",18000);
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            int imgId = data.getIntExtra("IMG",R.drawable.dog1);
            Student s = new Student(no,name,age,imgId);
            studentArrayList.add(s);

            adapter.notifyDataSetChanged();
        }
        else if (resultCode==3001){
            Log.e("eeee","eeeeee");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.dog1);
            int no = data.getIntExtra("NO", 0);

            for (int i=0;i<studentArrayList.size();i++){
               if (studentArrayList.get(i).getNo()== no){
                   Student s = studentArrayList.get(i);
                   s.setName(name);
                   s.setAge(age);
                   s.setImgid(imgId);

                   adapter.notifyDataSetChanged();
               }

            }
        }



    }

    @Override
    public void del(Student student) {
        studentArrayList.remove(student);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void edt(int i) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,EditActivity.class);
        intent.putExtra("NO",studentArrayList.get(i).getNo());
        intent.putExtra("NAME",studentArrayList.get(i).getName());
        intent.putExtra("AGE",studentArrayList.get(i).getAge());
        intent.putExtra("IMG",studentArrayList.get(i).getImgid());
        startActivityForResult(intent,2001);
    }
}
